package weather;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class StatisticsDisplay implements Observer, DisplayElement {

    ArrayList<Float> temps;
    ArrayList<Float> hums;
    ArrayList<Float> press;
    Observable observable;

    public StatisticsDisplay(Observable observable) {
        temps = new ArrayList<Float>();
        hums = new ArrayList<Float>();
        press = new ArrayList<Float>();
        this.observable = observable;
        observable.addObserver(this);
    }

    public void update(Observable obs, Object arg) {
        if (obs instanceof WeatherData) {
            WeatherData weather = (WeatherData) obs;
            this.temps.add(weather.getTemperature());
            this.hums.add(weather.getHumidity());
            this.press.add(weather.getPressure());
            display();
        }
    }

    private float getAverage() {
        float total = 0;
        for (Float tem : temps) {
            total = total + tem;
        }
        return total / temps.size();
    }

    private float getMin() {
        float min = temps.get(0);
        for (Float tem : temps) {
            if (tem < min) {
                min = tem;
            }
        }
        return min;
    }

    private float getMax() {
        float max = temps.get(0);
        for (Float tem : temps) {
            if (tem > max) {
                max = tem;
            }
        }
        return max;
    }

    public void display() {
        System.out.println("++ Avg/Max/Min temperature: " + this.getAverage() + "/"
                + this.getMax() + "/" + this.getMin());
    }
}